﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Devices;
using WP7_Barcode_Library;
using System.Windows.Navigation;


namespace MoviTiendas.Interfaz
{
    public partial class PaginaCamaraBarCode : PhoneApplicationPage
    {
        private PhotoCamera cam;
        private VideoBrush brush;

        public PaginaCamaraBarCode()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(PaginaCargada);
        }

        void PaginaCargada(object sender, RoutedEventArgs e)
        {
            cam = new PhotoCamera(CameraType.Primary);
            brush = new VideoBrush();
            brush.SetSource(cam);
            rectCamara.Fill = brush;
        }

        private void btnEscanear_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                WP7BarcodeManager.ScanMode = com.google.zxing.BarcodeFormat.UPC_EAN;

                if (false == true) //Use Barcode Sample Chooser "Task" if bypass camera setting is active (Emulator/demo mode)
                {
                  //  NavigationService.Navigating += new NavigatingCancelEventHandler(NavigationService_Navigating);
                  //  NavigationService.Navigate(BarcodeSampleItemManager.BarcodeChooserURI); //Navigate to barcode chooser page located in WP7_Barcode_Library.WP7Code
                }
                else //Use BarcodeManager to start camera and scan image
                {
                    WP7BarcodeManager.ScanBarcode(BarcodeResults_Finished); //Provide callback method
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error processing image.");
            }

        }


        public void BarcodeResults_Finished(BarcodeCaptureResult BCResults)
        {
            try
            {
                if (WP7BarcodeManager.LastCaptureResults.BarcodeImage != null)
                {
                   // imgCapture.Source = WP7BarcodeManager.LastCaptureResults.BarcodeImage; //Display image
                 
                }
                else
                {
                    //No image captured
                }
                //if (BCResults.Result == BarcodeResult.Success)
                if(BCResults != null && BCResults.BarcodeText != null)
                {
                    String codigo = BCResults.BarcodeText; //Use results
                    ParametroPaginas.reiniciarParametros("BarCode");
                    ParametroPaginas.agregarParametro(codigo);
                    NavigationService.GoBack();
                }
                else
                {
                    MessageBox.Show(BCResults.ErrorMessage);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format("Barcode Processing Error: {0}", ex.Message));
            }

        }

    }
}